package com.peanut.service.impl;

import com.peanut.domain.bo.ForumTitleBO;
import com.peanut.domain.po.ForumTitle;
import com.peanut.domain.po.TabAdmin;
import com.peanut.service.ForumTitleService;
import com.peanut.util.IdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ForumTitleServiceImpl implements ForumTitleService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    @Override
    public boolean insertTitle(ForumTitleBO bo, TabAdmin tabAdmin) {
        bo.setForumId(idWorker.nextId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        bo.setDate(sdf.format(date));
        bo.setLike(0L);
        bo.setCommentNum(0L);
        ForumTitle forumTitle = new ForumTitle();
        //BO复制到PO实体类
        BeanUtils.copyProperties(bo,forumTitle);
        mongoTemplate.insert(forumTitle);
        return true;
    }

    @Override
    public List<ForumTitleBO> getTitles(ForumTitleBO bo,Integer pageNum, Integer pageSize) {
        List<ForumTitle> forumTitles = null;
        //如果没输入标题，就全部查询
        if (bo.getForumTitle() == null){
            forumTitles = mongoTemplate.find(
                             new Query()
                            .skip(pageNum * pageSize)
                            .limit(pageSize)
                            .with(Sort.by(Sort.Order.desc("like"))), ForumTitle.class);
        }else{
        //如果输入了标题就模糊查询
            forumTitles = mongoTemplate.find(
                    new Query(Criteria.where("forum_title").regex(bo.getForumTitle()))
                            .skip(pageNum * pageSize)
                            .limit(pageSize)
                            .with(Sort.by(Sort.Order.desc("like"))), ForumTitle.class);
        }
        List<ForumTitleBO> voList = new ArrayList<>();
        for (ForumTitle forumTitle : forumTitles) {
            ForumTitleBO vo = new ForumTitleBO();
            BeanUtils.copyProperties(forumTitle,vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public ForumTitle selectTitleById(String forumId) {
        return mongoTemplate.findById(forumId,ForumTitle.class);
    }

    //帖子有一个评论，评论数就加一
    @Override
    public void updateCommentNum(String id) {
        Query query = new Query(Criteria.where("_id").is(id));
        Update update = new Update();
        update.inc("comment_num",1L);
        mongoTemplate.updateFirst(query,update,ForumTitle.class);
    }

    @Override
    public boolean delete(String id) {
        Query query = new Query(Criteria.where("_id").is(id));
        mongoTemplate.remove(query, ForumTitle.class);
        return true;
    }

    //删除多少评论+子评论，帖子评论数就减去多少
    @Override
    public boolean updateCommentNumber(String forumId, Long b) {
        Query query = new Query(Criteria.where("_id").is(forumId));
        Update update = new Update();
        update.inc("comment_num",b);
        mongoTemplate.updateFirst(query,update,ForumTitle.class);
        return true;
    }

    @Override
    public void likeTitle(String id) {
        Query query = new Query(Criteria.where("_id").is(id));
        Update update = new Update();
        update.inc("like",1L);
        mongoTemplate.updateFirst(query,update,ForumTitle.class);
    }
}
